﻿namespace PlaylistCopy.Helpers.Windows.Implementations
{
    using System.Collections.ObjectModel;
    using System.Linq;
    using CuttingEdge.Conditions;
    using PlaylistCopy.Logging;
    using PlaylistCopy.ViewModels;
    using System.Windows;
    using PlaylistCopy.ViewModels.Impl;
    using Condition = CuttingEdge.Conditions.Condition;

    internal class SummaryWindowOptions : ISummaryWindowOptions
    {
        private readonly Window summaryWindow;

        public SummaryWindowOptions(Window summaryWindow)
        {
            Condition.Requires(summaryWindow, "summaryWindow").IsNotNull();

            this.summaryWindow = summaryWindow;
        }

        private ISummaryWindowVm WindowViewModel
        {
            get { return this.summaryWindow.DataContext as ISummaryWindowVm; }
        }

        public void Logs(BufferedLogger bufferedLogger)
        {
            Condition.Requires(bufferedLogger, "bufferedLogger").IsNotNull();

            if (this.WindowViewModel.Logs == null)
            {
                this.WindowViewModel.Logs = new ObservableCollection<BufferedLogger.BufferedLoggerEntry>();
            }

            var logsColl = this.WindowViewModel.Logs;
            logsColl.Clear();

            foreach (var log in bufferedLogger.Buffer)
            {
                logsColl.Add(log);
            }
        }

        public void Status(SummaryStatus status)
        {
            this.WindowViewModel.Status = status;
        }

        public void ShowDialog()
        {
            this.summaryWindow.ShowDialog();
        }
    }
}
